﻿using System;
using System.Web.UI.WebControls;
using Service;

namespace ECommerce.Administration.Controls
{
	public partial class ProductAdditionalProperty : AdminBaseUsercontrol
	{
		private int _ProductID;
		private IProductAdditionalPropertyService _ProductAdditionalPropertyService;

		public int ProductID
		{
			get
			{
				if (ViewState["ProductID"] != null)
				{
					_ProductID = (int)ViewState["ProductID"];

					return _ProductID;
				}

				_ProductID = CommonHelper.QueryStringInt("productID");
				return _ProductID;
			}
			set
			{
				ViewState["ProductID"] = value;
				_ProductID = value;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				ButtonAddProperty.Text = GetLocalResourceString("Admin.AddButtonText");
			}
		}

		protected void GridAdditionalProperties_RowUpdating(object sender, GridViewUpdateEventArgs e)
		{
			var textDisplayOrder = GridAdditionalProperties.Rows[e.RowIndex].FindControl("TextDisplayOrder") as TextBox;
			var theProductAdditionalPropertyID = (int)GridAdditionalProperties.DataKeys[e.RowIndex].Values["ProductAdditionalPropertyID"];
			var TextPropertyValue = GridAdditionalProperties.Rows[e.RowIndex].FindControl("TextPropertyValue") as TextBox;
			int displayOrder = 0;
			if (int.TryParse(textDisplayOrder.Text.Trim(), out displayOrder))
			{
				var theProductAdditionalProperty = new Data.Entity.ProductAdditionalProperty();

				theProductAdditionalProperty.DisplayOrder = displayOrder;
				theProductAdditionalProperty.ProductAdditionalPropertyID = theProductAdditionalPropertyID;
				theProductAdditionalProperty.ProductID = ProductID;
				theProductAdditionalProperty.PropertyName = GridAdditionalProperties.Rows[e.RowIndex].Cells[0].Text.Trim();
				theProductAdditionalProperty.PropertyValue = TextPropertyValue.Text.Trim();

				if (_ProductAdditionalPropertyService == null)
				{
					_ProductAdditionalPropertyService = new Service.ProductAdditionalPropertyService();
				}

				_ProductAdditionalPropertyService.UpdateProductAdditionalProperty(theProductAdditionalProperty);
			}
		}

		protected void GridAdditionalProperties_RowDeleting(object sender, GridViewDeleteEventArgs e)
		{
			int additionalPropertyID = (int)GridAdditionalProperties.DataKeys[e.RowIndex]["ProductAdditionalPropertyID"];

			if (_ProductAdditionalPropertyService == null)
			{
				_ProductAdditionalPropertyService = new ProductAdditionalPropertyService();
			}

			_ProductAdditionalPropertyService.DeleteProductAdditionalProperty(additionalPropertyID);
		}

		protected void GridAdditionalProperties_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[0].Text = GetLocalResourceString("Admin.ProductAdditionalProperty.PropertyNameHeader");
				e.Row.Cells[1].Text = GetLocalResourceString("Admin.ProductAdditionalProperty.PropertyValueHeader");
				e.Row.Cells[2].Text = GetLocalResourceString("Admin.ProductAdditionalProperty.DisplayOrderHeader");
			}

			if (e.Row.DataItem != null)
			{
				var itemData = e.Row.DataItem as Data.Entity.ProductAdditionalProperty;
				var LinkButtonDelete = e.Row.FindControl("LinkButtonDelete") as LinkButton;
				var ButtonUpdate = e.Row.FindControl("ButtonUpdate") as Button;
				var TextDisplayOrder = e.Row.FindControl("TextDisplayOrder") as TextBox;

				LinkButtonDelete.Text = GetLocalResourceString("Admin.DeleteButtonText");
				ButtonUpdate.Text = GetLocalResourceString("Admin.UpdateButtonText");
				TextDisplayOrder.Text = itemData.DisplayOrder.ToString();
			}
		}

		protected void ButtonAddProperty_Click(object sender, EventArgs e)
		{
			if (_ProductAdditionalPropertyService == null)
			{
				_ProductAdditionalPropertyService = new ProductAdditionalPropertyService();
			}

			var theNewAdditionalProderty = new Data.Entity.ProductAdditionalProperty();
			int displayorder = 0;

			int.TryParse(TextDisplayOrder.Text.Trim(), out displayorder);

			theNewAdditionalProderty.DisplayOrder = displayorder;
			theNewAdditionalProderty.ProductID = ProductID;
			theNewAdditionalProderty.PropertyName = TextPropertyName.Text.Trim();
			theNewAdditionalProderty.PropertyValue = TextPropertyValue.Text.Trim();

			_ProductAdditionalPropertyService.CreateNewProductAdditionalProperty(theNewAdditionalProderty);
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			if (ProductID > 0)
			{
				PanelMain.Visible = true;
				pnlMessage.Visible = false;

				BindAdditionalProperties();
			}
			else
			{
				PanelMain.Visible = false;
				pnlMessage.Visible = true;
			}
		}

		private void BindAdditionalProperties()
		{
			if (_ProductAdditionalPropertyService == null)
			{
				_ProductAdditionalPropertyService = new ProductAdditionalPropertyService();
			}

			var data = _ProductAdditionalPropertyService.GetProductAdditionalProperties(ProductID);

			GridAdditionalProperties.DataSource = data;
			GridAdditionalProperties.DataBind();
		}
	}
}